package com.iwdnb.gkgz.infrastructure.dao.convert;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import cn.hutool.core.date.DateUtil;
import com.iwdnb.bmnf.common.convert.ToolConverter;
import com.iwdnb.bmnf.common.utils.BeanConvertUtils;
import com.iwdnb.gkgz.common.model.dto.StockDayData;
import com.iwdnb.gkgz.infrastructure.dao.model.entity.StockDayDataDO;
import org.apache.commons.collections4.CollectionUtils;
import org.mapstruct.Mapper;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.factory.Mappers;

@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE, uses = {ToolConverter.class})
public interface StockDayDataConverter {

    StockDayDataConverter INSTANCE = Mappers.getMapper(StockDayDataConverter.class);

    default List<StockDayData> toDTOs(List<StockDayDataDO> stockDayDataDOs) {
        if(CollectionUtils.isEmpty(stockDayDataDOs)){
            return null;
        }
        return stockDayDataDOs.stream().map(stockDayDataDO -> toDTO(stockDayDataDO)).collect(Collectors.toList());
    }

    default List<StockDayDataDO> toDOs(List<StockDayData> stockDayDatas) {
        if(CollectionUtils.isEmpty(stockDayDatas)){
            return null;
        }
        return stockDayDatas.stream().map(stockDayData -> toDO(stockDayData)).collect(Collectors.toList());
    }

    default StockDayData toDTO(StockDayDataDO stockDayDataDO) {
        if(Objects.isNull(stockDayDataDO)){
            return null;
        }
        StockDayData stockDayData = BeanConvertUtils.convert(stockDayDataDO, StockDayData.class);
        stockDayData.setDate(DateUtil.formatDate(stockDayDataDO.getDate()));
        return stockDayData;
    }

    default StockDayDataDO toDO(StockDayData stockDayData) {
        if(Objects.isNull(stockDayData)){
            return null;
        }
        StockDayDataDO stockDayDataDO = BeanConvertUtils.convert(stockDayData, StockDayDataDO.class);
        stockDayDataDO.setDate(DateUtil.parseDate(stockDayData.getDate()));
        return stockDayDataDO;
    }

}
